<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #root1 {
      margin-top: 100px;
      border-top: 1px solid #eee;
    }
  </style>
</head>
<body>
  <div id="root"></div>
  <script src="../js/react.development.js"></script>
  <script src="../js/react-dom.development.js"></script>
  <script src="../js/babel.min.js"></script>
  <script type="text/babel">
    class Count extends React.Component {
      
      constructor(props) {
        super(props)
        console.log('Count---constructor')

        this.state = {
          count: 1
        }
      }

      // componentWillMount() {
      //   console.log('Count---componentWillMount')
      // }
      UNSAFE_componentWillMount() {
        console.log('Count---UNSAFE_componentWillMount')
      }

      componentDidMount() {
        console.log('Count--componentDidMount')
      }

      // setState 更新
      // 必须只能返回 true/false
      // 注意：初始化时不会被调用，之后每次更新时才会被调用
      shouldComponentUpdate(props, state) {
        console.log('Count---shouldComponentUpdate', props, state)
        return true
      }

      componentWillUpdate(nextProps, nextState) {
        console.log('Count---componentWillUpdate', nextProps, nextState)
      }

      componentDidUpdate() {
        console.log('Count---componentDidUpdate')
      }

      componentWillUnmount() {
        console.log('Count---componentWillUnmount')
      }

      handleAddCount = (e) => {
        const { count } = this.state
        this.setState({
          count: count + 1
        })
      }

      handleUnMount = (e) => {
        ReactDOM.unmountComponentAtNode(document.getElementById('root'))
      }

      handleForceUpdate = (e) => {
        // 强制更新，不受 shouldComponentUpdate() 开关的影响，因为就不会调用此开关
        this.forceUpdate()
      }

      // 初始化执行一次，之后每次状态更新都会执行
      render() {
        console.log('Count---render')
        return (
          <div style={{ fontSize: '18px', fontWeight: 700 }}>
            <h3>当前 count 值为 { this.state.count }</h3>
            <button onClick={this.handleAddCount}>count + 1</button>
            <button onClick={this.handleUnMount}>卸载组件</button>
            <button onClick={this.handleForceUpdate}>不更改状态，强制更新组件</button>
          </div>
        )
      }
    }
    ReactDOM.render(<Count />, document.getElementById('root'))
  </script>


  <!-- props 改变时引起的更新 -->
  <div id="root1"></div>
  <script type="text/babel">
    class A extends React.Component {

      constructor(props) {
        console.log('A---constructor before super')
        super(props)
        console.log('A---constructor')
        this.state = {
          count: 1
        }
      }

      componentWillMount() {
        console.log('A---componentWillMount')
      }

      componentDidMount() {
        console.log('A---componentDidMount')
      }

      shouldComponentUpdate() {
        console.log('A---shouldComponentUpdate')
        return true
      }

      componentWillUpdate() {
        console.log('A---componentWillUpdate')
      }

      componentDidUpdate() {
        console.log('A---componentDidUpdate')
      }

      handleClick = (e) => {
        this.setState({
          count: this.state.count+1
        })
      }

      render() {
        console.log('A---render')
        return (
          <div>
            我是组件 A，传给组件 B 的值是 { this.state.count }
            <B count={this.state.count} />
            <button onClick={this.handleClick}>count+1</button>
          </div>
        )
      }
    }

    class B extends React.Component {

      constructor(props) {
        console.log('B---constructor before super')
        super(props)
        console.log('B---constructor')
      }

      componentWillMount() {
        console.log('B---componentWillMount')
      }

      componentDidMount() {
        console.log('B---componentDidMount')
      }

      // props 更新
      componentWillReceiveProps() {
        console.log('B---componentWillReceiveProps')
      }

      shouldComponentUpdate() {
        console.log('B---shouldComponentUpdate')
        return true
      }

      componentWillUpdate() {
        console.log('B---componentWillUpdate')
      }

      componentDidUpdate() {
        console.log('B---componentDidUpdate')
      }

      render() {
        console.log('B---render')
        return (
          <div>
            我是组件 B，来自组件 A 的值是 { this.props.count }
          </div>
        )
      }
    }
    ReactDOM.render(<A/>, document.getElementById('root1'))
  </script>
</body>
</html>